package com.koro.aspect;

import com.koro.tool.ValidatorUtils;
import com.koro.vo.ResponseObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 请求参数对象处理类
 */
@Aspect
@Component
public class ValidateRequestObjectAspect {

    @Pointcut("@annotation(com.koro.annotation.ValidateRequestObject)")
    public void validatePointCut() {
    }

    @Around("validatePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if (args != null) {
            for (Object arg : args) {
                ResponseObject responseObject = ValidatorUtils
                        .validateEntityWithResponse(arg);
                if (responseObject != null) {
                    return responseObject;
                }
            }
        }
        return point.proceed();
    }

}
